package com.automobile.service.impl;

import java.awt.print.Pageable;
import java.math.BigDecimal;
import java.util.List;

import com.automobile.domain.Img;
import org.apache.ibatis.session.SqlSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.automobile.dao.AnnounceDao;
import com.automobile.domain.Announce;
import com.automobile.domain.BeanPage;
import com.automobile.service.AnnounceService;
import com.automobile.service.CategoryService;

@CacheConfig(cacheManager = "redisCacheManager",cacheNames = {"announce"})
@Service("announceService")
public class AnnounceServiceImpl implements AnnounceService{

    @Qualifier("announceDao")
    @Autowired
	private AnnounceDao announceDao;
	
	@Autowired
	private CategoryService categoryService;

    /**
     * 用户发布的商品
     * @param userId
     * @return
     */
	public List<Announce> findUserPaste(int userId) {
		List<Announce> announces = announceDao.findAnnounceByUserId(userId);
		return announces;
	}

    /**
     * 首页展示查询的商品
     * @param beanPage
     * @return
     */
	public BeanPage findPaste(BeanPage beanPage) {
		//模糊查询
		if(beanPage.getTitle()!=null) {
			beanPage.setTitle("%"+beanPage.getTitle()+"%");
		}
		//计算显示第几页，每页12条数据
		beanPage.setPageCount((beanPage.getPageCount()-1)*12);
		beanPage.setPageSize(12);
		
		//如又限制价格，则需要将字符串找到相对于的id
		if(beanPage.getMoney()!=null)
			beanPage.setMoney(categoryService.findCategoryIdByName(beanPage.getMoney())+"");
		
		//总共又多少条数据
		int totalCount = announceDao.findAnnounceCountNum(beanPage);
		beanPage.setTotalCount(totalCount);
		
		//查询announce
		List<Announce> announces = announceDao.findAnnounce(beanPage);
		beanPage.setAnnounces(announces);
		
		System.out.println(beanPage);
		
		return beanPage;
	}

    /**
     * 查询某个商品
     * @param announceId
     * @return
     */
	@Cacheable(key = "#announceId")
	public Announce findAnnounceById(int announceId) {
		Announce announce = announceDao.findAnnounceById(announceId);
        return announce;
	}


	public int save(Announce announce) {
		// TODO Auto-generated method stub
		return announceDao.save(announce);
	}

	@CachePut(key = "#result.id")
	public Announce update(Announce announce) {
		// TODO Auto-generated method stub
		return announceDao.update(announce);
	}

	@CacheEvict(key = "#id")
	public void delete(int id) {
		announceDao.delete(id);
	}



}
